/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 14, 2012  11:46:16 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : FiLivOutgoingpaymentInbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.FiLivOutgoingpaymentInb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class FiLivOutgoingpaymentInbServices extends JPAService {

	private static final Logger log = Logger.getLogger(FiLivOutgoingpaymentInbServices.class);
	
	/**
	 * @param em
	 */
	public FiLivOutgoingpaymentInbServices(EntityManager em) {
		super(em);
	}

	public List<FiLivOutgoingpaymentInb> findAll() throws Exception {		
		
		return super.ConvertToYoureObject(super.findAll("FiLivOutgoingpaymentInb.findAll"));
	}

	public List<FiLivOutgoingpaymentInb> findByInstanceNo1(String instanceNo) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("instanceNo", instanceNo);
		
		return super.ConvertToYoureObject(super.find("FiLivOutgoingpaymentInb.findByInstanceNo", mapWhereValue));
	}

	public List<FiLivOutgoingpaymentInb> findBySAPDocumentNo(String instanceNo, String extRefNo, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("instanceNo", instanceNo);
		mapWhereValue.put("extRefNo", extRefNo);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiLivOutgoingpaymentInb.findBySAPDocumentNo", mapWhereValue));
	}
	
	public List<FiLivOutgoingpaymentInb> findBySAPDocumentNo(String  sapPlantCode, String instanceNo, String extRefNo, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("instanceNo", instanceNo);
		mapWhereValue.put("extRefNo", extRefNo);
		mapWhereValue.put("sapPlantCode", sapPlantCode);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiLivOutgoingpaymentInb.findBySAPDocumentNo1", mapWhereValue));
	}

	public FiLivOutgoingpaymentInb findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "FiLivOutgoingpaymentInb.findById"));
	}
	
	public FiLivOutgoingpaymentInb Updated(FiLivOutgoingpaymentInb value) throws Exception {
		if(value != null)
		{
			return (FiLivOutgoingpaymentInb) super.updated(value);
		}else
			return null;
	}
	
	public List<FiLivOutgoingpaymentInb> findIsSync(String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiLivOutgoingpaymentInb.findIsSync", mapWhereValue));		
	}
	
	public List<FiLivOutgoingpaymentInb> findByRequestNo(String sapPlantCode, String requestNo, String status, String instanceNo) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("sapPlantCode", sapPlantCode);	log.info("sapPlantCode =" + sapPlantCode);
		mapWhereValue.put("requestNo", requestNo);			log.info("requestNo =" + requestNo);
		mapWhereValue.put("status", status);				log.info("status =" + status);
		mapWhereValue.put("instanceNo", instanceNo);		log.info("instanceNo =" + instanceNo);
		
		return super.ConvertToYoureObject(super.find("FiLivOutgoingpaymentInb.findByRequestNo", mapWhereValue));		
	}
	
	public FiLivOutgoingpaymentInb InsertedOrUpdated(FiLivOutgoingpaymentInb vlaue) throws Exception {
		FiLivOutgoingpaymentInb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("FI_LIV_OUTGOINGPAYMENT_INB");					
				obj.setPromisStagingId(id);
						
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				
				if(CommonService.IsNull(obj.getUpdatedOn()))
					obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
				else
					obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
				
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<FiLivOutgoingpaymentInb> Inserted(List<FiLivOutgoingpaymentInb> vlauelist) throws Exception {
		log.info("Inserted ====> ");
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiLivOutgoingpaymentInb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("FI_LIV_OUTGOINGPAYMENT_INB");					
					obj.setPromisStagingId(id);
							
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					
					if(CommonService.IsNull(obj.getUpdatedOn()))
						obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
					else
						obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
					
					log.info(obj.toString());
					common_lst.add(obj);
				}	
			}
						
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<FiLivOutgoingpaymentInb> Updated(List<FiLivOutgoingpaymentInb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiLivOutgoingpaymentInb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
}
